package com.taptech.doufu.ui.view.topicview.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.ui.view.choice.ChoiceUtils;
import com.taptech.doufu.ui.view.topicview.ContentType;
import com.taptech.doufu.ui.view.topicview.UmengEventUtil;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class NovelTopicComicGridAdapter extends RecyclerView.Adapter<NovelTopicComicGridAdapter.ViewHolder>{

    private List<DFHomeNovelBeans> list;
    private Context context;
    private int picWidth;
    private int picHeight;
    private String type;
    private EventBean eventData;
    private ContentType contentType;
    private String pageType;
    public NovelTopicComicGridAdapter(Context context, List<DFHomeNovelBeans> list, ContentType contentType, EventBean eventData, String type,String pageType){
        this.list = list;
        this.context = context;
        this.eventData = eventData;
        this.type = type;
        this.contentType = contentType;
        this.pageType = pageType;
        picHeight = ChoiceUtils.getHeight(context);
        picWidth = ChoiceUtils.getPicWidth(context);
    }

    public DFHomeNovelBeans getItem(int position){
        if (list != null && list.size()>position) {
            return list.get(position);
        }
        return null;
    }



    public void refrush(List<DFHomeNovelBeans> list) {
        if (list != null) {
            this.list = list;
        }else {
            this.list.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.topic_novel_comic_grid_item, null);
        ViewHolder holder = new ViewHolder(view);
        holder.title = view.findViewById(R.id.title);
        holder.ivPic = view.findViewById(R.id.ivPic);
        holder.lay = view.findViewById(R.id.lay);
        holder.tvTag = view.findViewById(R.id.tvTag);
        RelativeLayout.LayoutParams picParams = new RelativeLayout.LayoutParams(picWidth,picHeight);
        holder.ivPic.setLayoutParams(picParams);
        holder.title.setWidth(picWidth + ScreenUtil.dip2px(context, 4));
        return holder;
    }



    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final DFHomeNovelBeans model = getItem(position);
        if (model != null) {
            holder.title.setText(model.getTitle());

            if (model.getImage() != null) {
                GlideUtil.displayRoundImage(context, holder.ivPic, model.getImage(),3,R.drawable.img_default_loading_l);
            }
            holder.lay.setOnClickListener(v -> {
                UmengEventUtil.addContentEvent(context,eventData,type,pageType);
                CartoonServices.enterCartoonDes(context,String.valueOf(model.getId()));
            });

            if (model.getTag_list() != null) {
                holder.tvTag.setVisibility(View.VISIBLE);
                holder.tvTag.setText((model.getTag_list()));
            }

            changeDayModelBg(holder);
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView ivPic;
        TextView tvTag;
        View lay;
        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder);
        }else {
            dayBg(viewHolder);
        }
    }

    private void dayBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.white));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.text_color_1));
        viewHolder.ivPic.setAlpha(1.0f);
    }

    private void darkBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        viewHolder.title.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        viewHolder.ivPic.setAlpha(Constant.imagViewAlpha);
    }
}
